from device import DeviceBase
from .tasks.hatch_eggs import HatchEggsTask
from interface.interface import Interface
from .config import KarDia_info

def dfs_interface_path(path:list[str], goal:str) -> bool:
    if path[-1] not in KarDia_info['interfaces'].keys():
        return False
    for i in KarDia_info['interfaces'][path[-1]].keys():
        if i in path:
            continue
        if i == goal:
            return True
        path.append(i)
        isOK = dfs_interface_path(path, goal)
        if isOK:
            return True
        path.pop()
    return False

class KarDia_tou_ABel(Interface):
    def __init__(self, device:DeviceBase):
        super().__init__('KarDia_tou_ABel', device)

        self.refresh_interface_info()
        self.add_task(HatchEggsTask(device))

    def change_interface(self, name:str):
        '''
        跳转到指定的界面
        '''
        self.refresh_interface_info()
        if self.interface_name == name:
            return
        path = [name]
        ans = dfs_interface_path(path, self.interface_name)
        while len(path) > 0:
            button_name = KarDia_info['interfaces'][path[-1]][self.interface_name]
            self.device.waiting_screen_changed(
                func=self.device.tap,
                # func=self.button_dict[button_name].tap,
                args=KarDia_info['interface_buttons'][self.interface_name][button_name]
                )
            self.refresh_interface_info(path[-1])
            path.pop()

    def change_city(self, name:str) -> bool:
        to_country = None
        for country, citys in KarDia_info['citys'].items():
            if name in citys:
                to_country = country
                break
        if to_country is None: return False
        self.change_interface('城镇')
        self.device.waiting_screen_changed(
            func=self.device.tap,
            args=KarDia_info['interface_buttons']['城镇']['大地图']
            )
        if len(self.device.find_image(KarDia_info['imgs'][to_country])) == 0:
            if to_country == '阿格莱恩':
                self.device.tap_image(KarDia_info['imgs']['返回阿格莱恩'], all=False)
            if to_country == '克里斯汀':
                self.device.swipe_up()
                self.device.swipe_right()
                self.device.tap_image(KarDia_info['imgs']['前往克里斯汀'], all=False)

        if self.device.tap_image(KarDia_info['imgs'][name], all=False) > 0:
            return True
        self.device.swipe_up()
        if self.device.tap_image(KarDia_info['imgs'][name], all=False) > 0:
            return True
        self.device.swipe_left()
        if self.device.tap_image(KarDia_info['imgs'][name], all=False) > 0:
            return True
        self.device.swipe_down()
        if self.device.tap_image(KarDia_info['imgs'][name], all=False) > 0:
            return True
        self.device.swipe_right()
        if self.device.tap_image(KarDia_info['imgs'][name], all=False) > 0:
            return True
        return False

    def refresh_interface_info(self, interface_name:str=None):
        '''
        刷新当前的界面信息
        '''
        self.device.screenshot()

        if interface_name:
            self.interface_name = interface_name
        else:
            # 刷新interface_name
            for interface_name, key in KarDia_info['interface_key'].items():
                if len(self.device.find_image(KarDia_info['imgs'][key])) > 0:
                    # 找到特征按钮
                    self.interface_name = interface_name
